//
//  XUAddPhotoController.m
//  新浪微博
//
//  Created by xuwenxin on 15-4-17.
//  Copyright (c) 2015年 xuwenxin. All rights reserved.
//

#import "XUAddPhotoController.h"
#import "XUSendPhotoCell.h"

@interface XUAddPhotoController () <XUSendPhotoCellDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
- (IBAction)addPhoto:(id)sender;

@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *layout;


@end

@implementation XUAddPhotoController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    CGFloat margin = 10;
    CGFloat cellWidth = ([UIScreen mainScreen].bounds.size.width - margin * 4) / 3;
    CGFloat cellHeit = cellWidth;
    
    self.layout.itemSize = CGSizeMake(cellWidth, cellHeit);
    self.layout.minimumInteritemSpacing = margin;
    self.layout.minimumLineSpacing = margin;
    
    self.layout.sectionInset = UIEdgeInsetsMake(margin, margin, margin, margin);
    
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark <UICollectionViewDataSource>

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.photos.count + 1;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    XUSendPhotoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"imgCell" forIndexPath:indexPath];
//    XUSendPhotoCell *cell = [[XUSendPhotoCell alloc] init];
    cell.delegate = self;
    
    if (self.photos.count == indexPath.item) {
        // 返回加号cell
        XUSendPhotoCell *btnCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
        return btnCell;
    } else {
        
        cell.img = self.photos[indexPath.item];
    }
    cell.path = indexPath.item;
    return cell;
}

- (void)XUSendPhotoCellDelegateClearPhotosWith:(NSInteger)path
{
    if (path < self.photos.count) {
        
        [self.photos removeObjectAtIndex:path];
        [self.collectionView reloadData];
    }
}

//- (void)XUSendPhotoCellDelegatePicker
//{
//    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
//    picker.delegate = self;
//    [self presentViewController:picker animated:YES completion:nil];
//}

// 点击相册图片打印的数据
/* info
 {
 UIImagePickerControllerMediaType = "public.image";
 UIImagePickerControllerOriginalImage = "<UIImage: 0x7fb90cdb9a30> size {834, 1250} orientation 0 scale 1.000000";
 UIImagePickerControllerReferenceURL = "assets-library://asset/asset.JPG?id=E2741A73-D185-44B6-A2E6-2D55F69CD088&ext=JPG";
 }
 20
 */
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [self.photos insertObject:info[UIImagePickerControllerOriginalImage] atIndex:0];
    [self.collectionView reloadData];
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [self dismissViewControllerAnimated:YES completion:nil];
}


- (NSMutableArray *)photos
{
    if (_photos == nil) {
        _photos = [NSMutableArray array];
    }
    return _photos;
}



#pragma mark <UICollectionViewDelegate>


- (IBAction)addPhoto:(id)sender {
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    [self presentViewController:picker animated:YES completion:nil];
}
@end
